import Vue from 'vue';

Vue.directive('drag', {
    update(el, binding) {
        let params = binding.value;
        el.onmousedown = function (en) {
            let ev = ev || event;
            let disX = en.clientX - el.offsetLeft;
            let disY = en.clientY - el.offsetTop;
            document.onmousemove = function (en) {
                let ev = ev || event;
                if (
                    en.clientY > 0 &&
                    en.clientY < params.clientHeight &&
                    en.clientX > 0 &&
                    en.clientX < params.clientWidth
                ) {
                    el.style.left = en.clientX - disX + 'px';
                    el.style.top = en.clientY - disY + 'px';
                }
            };
            document.onmouseup = function () {
                document.onmousemove = null;
                document.onmouseup = null;
            };
            return false;
        };
    }
});